package io.renren.modules.job.task;

import io.renren.modules.fetch.FTPUtil;
import io.renren.modules.file.entity.LocStoraEntity;
import io.renren.modules.file.entity.ServerNodeEntity;
import io.renren.modules.file.service.LocStoraService;
import io.renren.modules.file.service.ServerNodeService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;


/**
 * 文件抓取定时任务
 */
@Component
public class FileFatchTask implements ITask {

    @Autowired
    LocStoraService locStoraService;

    @Autowired
    ServerNodeService serverNodeService;

    public static final String path = "/ftpDown";

    @Override
    public void run(String params) {
        //得到文件存储路径
        List<LocStoraEntity> locStoraEntityList = locStoraService.list();
        LocStoraEntity locStoraEntity = null;
        if (locStoraEntityList != null && locStoraEntityList.size() >= 1) {
            locStoraEntity = locStoraEntityList.get(0);
        } else {
            locStoraEntity = new LocStoraEntity();
            locStoraEntity.setPath(path);
        }
        //得到节点信息
        List<ServerNodeEntity> nodeEntities = serverNodeService.list();
        FTPUtil ftpUtil = new FTPUtil();
        if (CollectionUtils.isEmpty(nodeEntities)) {
            return;
        }
         for (ServerNodeEntity entity : nodeEntities) {
//            ftpUtil.setAddress(entity.getIp());
//            ftpUtil.setPassword(entity.getPassword());
//            ftpUtil.setPort(entity.getPort());
//            ftpUtil.setUsername(entity.getPassword());
            ftpUtil.downloadDirWrap("",locStoraEntity.getPath()+"/"+entity.getName());
        }
    }


    public static void main(String[] args) {
        FTPUtil ftpUtil = new FTPUtil();
        ftpUtil.downloadDirWrap("","D:/ftpDown/节点1");
    }
}
